package net.wanho.shop.service.impl;

import net.wanho.shop.po.Cart;
import net.wanho.shop.mapper.CartMapper;
import net.wanho.shop.service.CartService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CartServiceImpl implements CartService {

    private final CartMapper cartMapper;

    /**
     * 购物车表分页查询
     * @param pageNum
     * @param pageSize
     * @param cart
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Cart cart) {
        if(ObjectUtil.isNull(cart)){
            cart = new Cart();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Cart> cartList = cartMapper.select(cart);
        return new PageInfo(cartList);
    }

    /**
     * 根据主键查询购物车表
     * @param cartId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Cart findById(Serializable cartId) {
        return cartMapper.selectById(cartId);
    }

    /**
     * 购物车表新增
     * @param cart
     */
    @Override
    public void add(Cart cart) {
        cartMapper.insert(cart);
    }

    /**
     * 修改购物车表
     * @param cart
     */
    @Override
    public void modify(Cart cart) {
        cartMapper.update(cart);
    }

    /**
     * 根据主键删除购物车表
     * @param cartId
     */
    @Override
    public void removeById(Serializable cartId) {
        if(((String)cartId).contains(",")){
            cartMapper.deleteByIds(((String)cartId).split(","));
        }else {
            cartMapper.deleteById(cartId);
        }
    }
}
